package com.example.todolist.service;

import com.example.todolist.entity.Task;
import com.example.todolist.mapper.TaskMapper;
import com.example.todolist.tools.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

@Service
public class TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private TaskEachDayService taskEachDayService;

    public ResponseBody<Integer> insert(Task task) {
        if (taskMapper.selectSameName(task.getUserId(),task.getTaskName()) == 0)
        {
            if (task.getBeginDate() == null)
            {
                task.setBeginDate(Date.valueOf(LocalDate.now()));
            }
            if(task.getTaskDone()==null)
            {
                task.setTaskDone(0);
            }
            taskMapper.insertByTask(task);
            int data=taskMapper.findByTaskName( task.getUserId(),task.getTaskName()).getTaskId();
            return new ResponseBody<>(200,"success",data);
        }
        else
            return new ResponseBody<>(-1,"the task name can not be repeated",null);
    }

    public ResponseBody<Integer> checkForTask(Integer userId,Integer taskId)
    {
        Date date = Date.valueOf(LocalDate.now());
        taskMapper.checkForTask(userId,taskId,date);
        taskEachDayService.updateTaskEachDay(userId);
        return new ResponseBody<>(200,"success",taskId);
    }
    public List<Task> getTypeTask(Integer userId,String taskType)
    {
        return taskMapper.getTypeTask(userId,taskType+"%");
    }
    public Integer update(Task task){
        return taskMapper.update(task);
    }


}
